package ch.uzh.ifi.sopra.gruppe9.rmi_test;

import java.rmi.ConnectException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class main {
	
	private static IPlayer myPlayer;
	private static IPlayer otherPlayer;
	
	private static IGame game;

	/**
	 * Game main function
	 */
	public static void main(String[] args) throws RemoteException {
		System.out.println("Game Started!");
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter IP address:");
		String ip = scanner.nextLine();
		Registry r;
		try {
			// Try being client
			try {
				r = LocateRegistry.getRegistry(ip);
				myPlayer = (IPlayer) r.lookup("playerTwo");
				otherPlayer = (IPlayer) r.lookup("playerOne");
				System.out.println("I'm player 2.");
			// Be server	
			} catch (ConnectException e) {
				Server.getInstance();
				r = LocateRegistry.getRegistry(ip);
				myPlayer = (IPlayer) r.lookup("playerOne");
				otherPlayer = (IPlayer) r.lookup("playerTwo");
				System.out.println("I'm player 1.");
			}
			game = (IGame) r.lookup("game");
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
		myPlayer.setReady(true);
		System.out.println("Connected!");
		
		while(!game.isReady());
		
		while(game.isReady()) {
			System.out.println("Your current balance: $" + myPlayer.getBalance());
			System.out.println("Your Enemy's balance is: $" + otherPlayer.getBalance());
			System.out.println("Welcome to the money printing company.");
			System.out.println("How much money would you like to print?");
			int increase = Integer.parseInt(scanner.nextLine());
			myPlayer.setMoneyIncrease(increase);
		}
		
	}

}
